var initVPS = function (userVpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '您确定要[初始化]这台该vps吗?',
        type: BootstrapDialog.TYPE_WARNING,
        closable: false,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doInitVPS(userVpsId);
            }
        }]
    });
}
var doInitVPS = function (userVpsId) {
    waitingDialog.show('操作中,请稍后...', {dialogSize: 'sm', progressType: 'wanring'});
    $.ajax({
        type: 'POST',
        url: "/doInitVPS",
        data: {userVpsId: userVpsId},
        success: function (result) {
            waitingDialog.hide();

            if (result.succ) {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    size: BootstrapDialog.SIZE_SMALL,
                    buttons: [{
                        label: 'OK',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            //   dialog.close();
                            location.reload(true);
                        }
                    }]
                });
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            // //alert("Data not found");
        }

    });
};

var disableUserVps = function (userVpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '您确定要[停用]该用户的这台该vps吗?',
        type: BootstrapDialog.TYPE_WARNING,
        closable: false,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doDisableUserVps(userVpsId);
            }
        }]
    });
}

/**
 * 停用
 * @param userVpsId
 */
var doDisableUserVps = function (userVpsId) {
    waitingDialog.show('停用中,请稍后...', {dialogSize: 'sm', progressType: 'wanring'});
    $.ajax({
        type: 'POST',
        url: "/disableUserVps",
        data: {userVpsId: userVpsId},
        success: function (result) {
            waitingDialog.hide();

            if (result.succ) {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    size: BootstrapDialog.SIZE_SMALL,
                    buttons: [{
                        label: 'OK',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            //   dialog.close();
                            location.reload(true);
                        }
                    }]
                });
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            // //alert("Data not found");
        }

    });
};


var switchTipDiv = function (vpsId, tipText) {
    var command_div = "#" + vpsId + "_command_div";
    var tip_div = "#" + vpsId + "_tip_div";
    var tip_text_div = "#" + vpsId + "_tip_div_text";

    $(command_div).hide();
    $(tip_div).show();
    $(tip_text_div).text(tipText);
}
var switchTipDivRecovery = function (vpsId) {
    var command_div = "#" + vpsId + "_command_div";
    var tip_div = "#" + vpsId + "_tip_div";
    var tip_text_div = "#" + vpsId + "_tip_div_text";

    $(command_div).show();
    $(tip_div).hide();
    //刷新vps状态
    refreshVpsState(vpsId);

}

var vpsStart = function (vpsId,field) {
        BootstrapDialog.show({
            title: '温馨提示',
            message: '您确定要进行系统【开机】吗?',
            type: BootstrapDialog.TYPE_WARNING,
            closable: false,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                cssClass: 'btn-primary',
                action: function (dialog) {
                    dialog.close();
                    doVpsStart(vpsId);
                }
            }]
        });
}
/**
 * Created by MJiao on 15/9/13.
 */
var doVpsStart = function (vpsId) {

    switchTipDiv(vpsId, "正在开机,请稍后...")

    //waitingDialog.show('正在开机,请稍后...', {dialogSize: 'sm', progressType: 'success'});
    $.ajax({
        type: 'POST',
        url: "/vpsStart",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            switchTipDivRecovery(vpsId);
            if (result.succ) {

                //BootstrapDialog.show({
                //    title: '温馨提示',
                //    message: result.msg,
                //    closable: false,
                //    size: BootstrapDialog.SIZE_SMALL,
                //    buttons: [{
                //        label: 'OK',
                //        cssClass: 'btn-primary',
                //        action: function (dialog) {
                //            //   dialog.close();
                //            location.reload(true);
                //        }
                //    }]
                //});
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
};
var resetartVps = function (vpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '您确定要系统【重启】吗?',
        type: BootstrapDialog.TYPE_WARNING,
        closable: false,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doRestartVps(vpsId);
            }
        }]
    });
}

var doRestartVps = function (vpsId) {
    switchTipDiv(vpsId, "正在重启,请稍后...")
    //waitingDialog.show('正在重启,请稍后...', {dialogSize: 'sm', progressType: 'warning'});
    $.ajax({
        type: 'POST',
        url: "/vpsRestart",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            switchTipDivRecovery(vpsId);
            if (result.succ) {

                //BootstrapDialog.show({
                //    title: '温馨提示',
                //    message: result.msg,
                //    closable: false,
                //    size: BootstrapDialog.SIZE_SMALL,
                //    buttons: [{
                //        label: 'OK',
                //        cssClass: 'btn-primary',
                //        action: function (dialog) {
                //            //   dialog.close();
                //            location.reload(true);
                //        }
                //    }]
                //});
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
}

var createSnapshot = function (vpsId) {
    waitingDialog.show('正在备份,请稍后...', {dialogSize: 'sm', progressType: 'warning'});
    $.ajax({
        type: 'POST',
        url: "/createSnapshot",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            if (result.succ) {
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    size: BootstrapDialog.SIZE_SMALL,
                    buttons: [{
                        label: 'OK',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            //   dialog.close();
                            location.reload(true);
                        }
                    }]
                });
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
};

var restoreSnapshot = function (vpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '您确定要进行系统【恢复】吗?',
        type: BootstrapDialog.TYPE_WARNING,
        closable: false,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doRestoreSnapshot(vpsId);
            }
        }]
    });
}
//恢复系统初始化
var initVpsSnapshot = function (vpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '您确定要初始化该主机吗?',
        type: BootstrapDialog.TYPE_WARNING,
        closable: false,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doInitVpsSnapshot(vpsId);
            }
        }]
    });
}

var doInitVpsSnapshot = function (vpsId) {
    switchTipDiv(vpsId, "系统初始化中,请稍后...");
    //waitingDialog.show('系统恢复中,请稍后...', {dialogSize: 'sm', progressType: 'warning'});
    $.ajax({
        type: 'POST',
        url: "/initVpsSnapshot",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            switchTipDivRecovery(vpsId);
            if (result.succ) {

                //BootstrapDialog.show({
                //    title: '温馨提示',
                //    message: result.msg,
                //    closable: false,
                //    size: BootstrapDialog.SIZE_SMALL,
                //    buttons: [{
                //        label: 'OK',
                //        cssClass: 'btn-primary',
                //        action: function (dialog) {
                //            //   dialog.close();
                //            location.reload(true);
                //        }
                //    }]
                //});
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
};
var doRestoreSnapshot = function (vpsId) {
    switchTipDiv(vpsId, "系统恢复中,请稍后...");
    //waitingDialog.show('系统恢复中,请稍后...', {dialogSize: 'sm', progressType: 'warning'});
    $.ajax({
        type: 'POST',
        url: "/restoreSnapshot",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            switchTipDivRecovery(vpsId);
            if (result.succ) {

                //BootstrapDialog.show({
                //    title: '温馨提示',
                //    message: result.msg,
                //    closable: false,
                //    size: BootstrapDialog.SIZE_SMALL,
                //    buttons: [{
                //        label: 'OK',
                //        cssClass: 'btn-primary',
                //        action: function (dialog) {
                //            //   dialog.close();
                //            location.reload(true);
                //        }
                //    }]
                //});
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
};

/**
 * 刷新vps状态
 * @param vpsId
 */
var refreshVpsState = function (vpsId) {
    $.ajax({
        type: 'POST',
        url: "/getVpsById",
        data: {vpsId: vpsId},
        success: function (result) {
            var vpsStateId = "#" + vpsId + "_vps_state";
            $(vpsStateId).text(result.status);
        },
        error: function (result) {

        }

    });
}

var vpsPowerOff = function (vpsId) {

    BootstrapDialog.show({
        title: '温馨提示',
        message: '关机后会vps无法提供服务,您确定要关机吗?',
        closable: false,
        type: BootstrapDialog.TYPE_WARNING,
        buttons: [{
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }, {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialog) {
                dialog.close();
                doVpsPowerOff(vpsId);
            }
        }]
    });
}

var doVpsPowerOff = function (vpsId) {
    switchTipDiv(vpsId, "正在关机,请稍后...");
    // waitingDialog.show('正在关机,请稍后...', {dialogSize: 'sm', progressType: 'warning'});
    $.ajax({
        type: 'POST',
        url: "/vpsPowerOff",
        data: {vpsId: vpsId},
        success: function (result) {
            waitingDialog.hide();
            switchTipDivRecovery(vpsId);
            if (result.succ) {

                //BootstrapDialog.show({
                //    title: '温馨提示',
                //    message: result.msg,
                //    closable: false,
                //    size: BootstrapDialog.SIZE_SMALL,
                //    buttons: [{
                //        label: 'OK',
                //        cssClass: 'btn-primary',
                //        action: function (dialog) {
                //            //   dialog.close();
                //            location.reload(true);
                //        }
                //    }]
                //});
            }
            else {

                BootstrapDialog.show({
                    title: '温馨提示',
                    message: result.msg,
                    closable: false,
                    type: BootstrapDialog.TYPE_DANGER,
                    buttons: [{
                        label: '知道了',
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }
                    ]
                });
            }
        },
        error: function (result) {
            //alert("Data not found");
        }

    });
};